package com.ihisun.tmhcms.core.service.sysuserservice.impl;

import com.ihisun.tmhcms.core.dao.sysroledao.EditSysRoleDao;
import com.ihisun.tmhcms.core.dao.sysroledao.SysRoleDao;
import com.ihisun.tmhcms.core.dao.sysuserdao.EditSysUserDao;
import com.ihisun.tmhcms.core.dao.sysmenudao.SysMenuDao;
import com.ihisun.tmhcms.core.dao.sysuserdao.SysUserDao;
import com.ihisun.tmhcms.core.dao.sysuserroledao.SysUserRoleDao;
import com.ihisun.tmhcms.core.dto.sysroledto.SysRoleDto;
import com.ihisun.tmhcms.core.dto.sysuserdto.EditUserPwdDto;
import com.ihisun.tmhcms.core.dto.sysuserdto.SysUserDto;
import com.ihisun.tmhcms.core.dto.sysuserroledto.SysUserRoleDto;
import com.ihisun.tmhcms.core.service.sysuserservice.SysUserService;
import com.ihisun.tmhcms.core.vo.SysMenuVo;
import com.ihisun.tmhcms.core.vo.SysRoleVo;
import com.ihisun.tmhcms.core.vo.SysUserVo;
import com.ihisun.tmhcms.utils.DateUtils;
import com.ihisun.tmhcms.utils.ResponseVo;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import sun.security.provider.MD5;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Resource
    SysMenuDao sysMenuDao;
    @Resource
    SysRoleDao sysRoleDao;
    @Resource
    EditSysRoleDao editSysRoleDao;
    @Resource
    EditSysUserDao editSysUserDao;
    @Resource
    SysUserDao sysUserDao;
    @Resource
    SysUserRoleDao sysUserRoleDao;
    @Override
    public ResponseVo jurisdiction() {
        List<SysMenuVo> byAll = sysMenuDao.findAll();
        return ResponseVo.ok(byAll);
    }

    @Override
    public ResponseVo findByRole() {
        List<SysRoleVo> byAll = sysRoleDao.findByAll();
        return ResponseVo.ok(byAll);
    }

    @Override
    public ResponseVo editRole(SysRoleDto sysRoleDto) {
        if (sysRoleDto.getRoleId()!=null){
            /**
             * 修改
             */
            sysRoleDto.setUpdateTime(DateUtils.getCurrentTime());
            editSysRoleDao.save(sysRoleDto);
        }else{
            /**
             * 添加
             */
            sysRoleDto.setUpdateTime(DateUtils.getCurrentTime());
            sysRoleDto.setCreateTime(DateUtils.getCurrentTime());
            editSysRoleDao.save(sysRoleDto);
        }
        return ResponseVo.ok();
    }

    @Transactional
    @Override
    public ResponseVo editUser(SysUserDto sysUserDto) {

        if (sysUserDto.getUserId()!=null){
            /**
             * 修改
             */
            sysUserDto.setUpdateTime(DateUtils.getCurrentTime());
            SysUserRoleDto byUserIdAndRoleId = sysUserRoleDao.findByUserIdAndRoleId(String.valueOf(sysUserDto.getUserId()), sysUserDto.getRoleId());
            byUserIdAndRoleId.setRoleId(sysUserDto.getRoleId());
            byUserIdAndRoleId.setUpdateTime(DateUtils.getCurrentTime());
            sysUserRoleDao.save(byUserIdAndRoleId);
            editSysUserDao.save(sysUserDto);
        }else{
            /**
             * 编辑
             */
            sysUserDto.setUpdateTime(DateUtils.getCurrentTime());
            sysUserDto.setCreateTime(DateUtils.getCurrentTime());
            editSysUserDao.save(sysUserDto);

            SysUserRoleDto sysUserRoleDto=new SysUserRoleDto();
            sysUserRoleDto.setUpdateTime(DateUtils.getCurrentTime());
            sysUserRoleDto.setRoleId(sysUserDto.getRoleId());
            sysUserRoleDto.setUserId(sysUserDto.getUserId().toString());
            sysUserRoleDto.setCreateTime(DateUtils.getCurrentTime());
            sysUserRoleDao.save(sysUserRoleDto);
        }


        return ResponseVo.ok();
    }

    public ResponseVo updateUserState(String id,String state){
        SysUserDto byUserId = editSysUserDao.findByUserId(Long.parseLong(id));
        byUserId.setStatus(state);
        byUserId.setUpdateTime(DateUtils.getCurrentTime());
        SysUserDto save = editSysUserDao.save(byUserId);
        return ResponseVo.ok();
    }

    @Override
    public ResponseVo sysUserList() {
        List<SysUserVo> byStatusNot = sysUserDao.findByStatusNot("3");
        return ResponseVo.ok(byStatusNot);
    }

    @Override
    public ResponseVo updatePassword(EditUserPwdDto editUserPwdDto) {
        SysUserDto byUserId = editSysUserDao.findByUserId(Long.parseLong(editUserPwdDto.getUserId()));
        byUserId.setPassword(DigestUtils.md5DigestAsHex(editUserPwdDto.getPassword().getBytes()));
        editSysUserDao.save(byUserId);
        return ResponseVo.ok();
    }
}
